import datetime
from storage import Storage
from html import *
import contrib.simplejson as simplejson
import contrib.rss2 as rss2

def xml_rec(value,key='document'):
    if isinstance(value, (dict, Storage)):
        return TAG[key](*[TAG[k](xml_rec(v,'')) for k, v in value.items()])
    elif isinstance(value, list):
        return TAG[''](*[TAG.item(xml_rec(item,'')) for item in value])
    elif value == None:
        return 'None'
    else:
        return str(value)

def xml(value):
    return str(xml_rec(value))

def json(value):
    return simplejson.dumps(value)

def csv(value):
    return ''

def rss(feed):
    if not 'entries' in feed and 'items' in feed:
        feed['entries'] = feed['items']
    now=datetime.datetime.now()
    rss = rss2.RSS2(title = feed['title'],
                    link = feed['link'],
                    description = feed['description'],
                    lastBuildDate = feed.get('created_on',now),
                    items = [rss2.RSSItem(title=entry['title'],
                                          link=entry['link'],
                                          description=entry['description'],
                                          pubDate=entry.get('created_on',now))\
                                 for entry in feed['entries']]
                    )
    return rss2.dumps(rss)
